package cn.village.controller;

import cn.village.entity.VaUser;
import cn.village.service.VaUserService;
import cn.village.utils.Jurisdiction;
import cn.village.utils.PageSupport;
import org.springframework.data.repository.query.Param;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (VaUser)表控制层
 *
 * @author makejava
 * @since 2021-02-02 10:50:42
 */
@Controller
public class VaUserController {
    /**
     * 服务对象
     */
    @Resource
    private VaUserService vaUserService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public VaUser selectOne(Integer id) {
        return this.vaUserService.queryById(id);
    }

    @RequestMapping("/village")
    public String login(){
        return "login";
    }

    @RequestMapping("/login")
    public String dologin(String userCode, String password, HttpSession session,
                          HttpServletRequest request){
        VaUser vu=vaUserService.findByName(userCode);
        if(vu!=null){
            if(password.equals(vu.getPassword())){
                session.setAttribute("userName",vu.getUsername());
                session.setAttribute("usercode",vu.getUsercode());
                session.setAttribute("roleId",vu.getRoleid());
                return "index";
            }else {
                String err="账号密码错误，请重新登录";
                request.setAttribute("loginErr",err);
                return "login";
            }
        }else {
            String err = "账号密码错误，请重新登录";
            request.setAttribute("loginErr", err);
            return "login";
        }
    }

    @RequestMapping("/admin_list")
    public String goAdminList(Integer roleId, Integer pageIndex, HttpSession session,
                              Model model,HttpServletRequest request){

        if(Jurisdiction.adminCheck(roleId)){
            Jurisdiction.err(request);
            return "welcome";
        }else {
            PageSupport p = new PageSupport();
            p.setPageSize(5);
            int totalCount = vaUserService.findCount();
            p.setTotalCount(totalCount);
            if (pageIndex == null) {
                pageIndex = 1;
            }
            p.setCurrentPageNo(pageIndex);
            Integer totalPageCount = p.getTotalPageCount();
            List<VaUser> user = vaUserService.findeAll(pageIndex);
            session.setAttribute("user", user);
            model.addAttribute("totalCount", totalCount);
            model.addAttribute("currentPageNo", p.getCurrentPageNo());
            model.addAttribute("totalPageNo", totalPageCount);
            return "admin-list";
        }
    }

    @RequestMapping("/admin_list_limit")
    public String goLimit(Integer pageIndex, HttpSession session, Model model){
        PageSupport p = new PageSupport();
        p.setPageSize(5);
        int totalCount = vaUserService.findCount();
        p.setTotalCount(totalCount);
        if (pageIndex == null) {
            pageIndex = 1;
        }
        p.setCurrentPageNo(pageIndex);
        Integer totalPageCount = p.getTotalPageCount();
        List<VaUser> user = vaUserService.findeAll(pageIndex);
        session.setAttribute("user", user);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("currentPageNo", p.getCurrentPageNo());
        model.addAttribute("totalPageNo", totalPageCount);
        return "admin-list";
    }

    @RequestMapping("/goupdata")
    public String goupdata(Integer id,Model model){
        VaUser vaUser=vaUserService.queryById(id);
        model.addAttribute("vaUserByid",vaUser);
        return "admin-edit";
    }

    @RequestMapping("/admin_updata")
    public String updata(VaUser vaUser){
        int row=vaUserService.update(vaUser);
        if(row>0){
            return "redirect:/admin_list";
        }
        return "admin-edit";
    }


    @RequestMapping("/admin_add")
    public String add(VaUser vaUser,HttpSession session){
        vaUser.setCreationdate(new Date());
        vaUser.setModifyby(session.getAttribute("usercode").toString());
        int row=vaUserService.insert(vaUser);
        if (row>0){
            return "redirect:/admin_list";
        }
        return "admin-add";
    }

    @RequestMapping("/goadd")
    public  String goadd(){
        return "admin-add";
    }

    @RequestMapping("/del")
    public String del(Integer id){
       if(vaUserService.deleteById(id)){
           return "redirect:/admin_list";
       }
       return "admin-list";
    }

    @RequestMapping("admin_find")
    public String findByName(@RequestParam String username,Integer pageIndex,HttpSession session,Model model){
        PageSupport p=new PageSupport();
        p.setPageSize(5);
        int totalCount=vaUserService.findCountByName(username);
        p.setTotalCount(totalCount);
        if(pageIndex==null){
            pageIndex=1;
        }
        p.setCurrentPageNo(pageIndex);
        Integer totalPageCount=p.getTotalPageCount();
        List<VaUser> list=vaUserService.queryByName(username);
        session.setAttribute("user",list);
        model.addAttribute("totalCount",totalCount);
        model.addAttribute("currentPageNo",p.getCurrentPageNo());
        model.addAttribute("totalPageNo",totalPageCount);
        return "admin-list";
    }
}
